package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.gui.*;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;

public class NursePresenter implements ActionListener{
	private DataBaseAccessor dba;
	private Nurse myNurse;
	private INurseView myView;
	
	public NursePresenter (Nurse nurse, INurseView nurseWindow) {
		myNurse = nurse;
		myView = nurseWindow;
		myView.addListener(this);
		myView.showSelf();
	}
	
	
	public NursePresenter(Nurse nurse, DataBaseAccessor db) {
		myNurse = nurse;
		dba = db;
		myView = new NurseWindow();
		myView.addListener(this);
		myView.showSelf();
	}


	public void actionPerformed(ActionEvent event) {
		
	
		if ( event.getSource() == myView.getExitButton()){
			dba.closeConnectionToDatabase();
			System.exit(0);
		}
		
		else if ( event.getSource() == myView.getInvoiceButton() ){
			System.out.println("Reports window");
			this.dba.connectToDatabase();
			ArrayList<Patient> patientList= dba.getAllPatients();
			System.out.println("Patients!");

			Patient[] patientArr= new Patient[patientList.size()];
			for(int i = 0; i<patientArr.length; i++){
				patientArr[i]= patientList.get(i);
			}
			
			String[] pNamesandEmails= new String[patientArr.length];
			for(int i = 0; i<patientArr.length; i++){
				pNamesandEmails[i]= patientArr[i].getName().toString() + " - " + patientArr[i].getEmail();
			}
			
			String chosen= (String)JOptionPane.showInputDialog(null, "Choose a patient.", "Invoice Patient", JOptionPane.QUESTION_MESSAGE, null, pNamesandEmails, pNamesandEmails[0]);
			if(chosen != null){
				int index= this.getChosenIndex(pNamesandEmails, chosen);
				Patient p= patientList.get(index);
				p = dba.find(Patient.class, p.getId());
				InvoicePresenter pres = new InvoicePresenter(this.dba, p);
			}
		}
		
		else if( event.getSource() == myView.getCreatePatientButton()){
			UserInfoPresenter pres= new UserInfoPresenter(0, new PatientInformationWindow());
		}
		
		else if( event.getSource() == myView.getDeletePatientButton()){
			ArrayList<Patient> patientList= dba.getAllPatients();
			Patient[] patientArr= new Patient[patientList.size()];
			for(int i = 0; i<patientArr.length; i++){
				patientArr[i]= patientList.get(i);
			}
			
			String[] pNamesandEmails= new String[patientArr.length];
			for(int i = 0; i<patientArr.length; i++){
				pNamesandEmails[i]= patientArr[i].getName().toString() + " - " + patientArr[i].getEmail();
			}
			
			String chosen= (String)JOptionPane.showInputDialog(null, "Choose a patient.", "Delete Patient", JOptionPane.QUESTION_MESSAGE, null, pNamesandEmails, pNamesandEmails[0]);
			if(chosen != null){
				int index= this.getChosenIndex(pNamesandEmails, chosen);
				System.out.println(index);
				
				Patient p= patientList.get(index);
				
				System.out.println(p.getEmail());
				
				dba.removeUser(p);
			}
		}
		
		else if( event.getSource() == myView.getEditPatientButton()){
			EditUserInfoPresenter pres= new EditUserInfoPresenter(0, new PatientInformationWindow());
		}
		
		else if( event.getSource() == myView.getSearchPatientButton()){
			FindByNamePresenter	pres= new FindByNamePresenter(new NameWindow());
		}
		
		else if( event.getSource() == myView.getViewPatientButton()){
			String[] columnNames={"Name", "id", "email", "position"};
			ArrayList<Patient> users= dba.getAllPatients();
			int rows= users.size();
			String[][] info= new String[rows][4];
			for(int a=0; a<rows; a++)
			{
				info[a][0]= users.get(a).getName().toString();
				info[a][1]= Long.toString(users.get(a).getId());
				info[a][2]= users.get(a).getEmail();
				info[a][3]= users.get(a).getType().toString();
			}
			
			ViewUsersPresenter view= new ViewUsersPresenter(info, columnNames);
		}
		
		else if( event.getSource() == myView.getTreatmentButton()){
			System.out.println("Reports window");
			this.dba.connectToDatabase();
			ArrayList<Patient> patientList= dba.getAllPatients();
			System.out.println("Patients!");

			Patient[] patientArr= new Patient[patientList.size()];
			for(int i = 0; i<patientArr.length; i++){
				patientArr[i]= patientList.get(i);
			}
			
			String[] pNamesandEmails= new String[patientArr.length];
			for(int i = 0; i<patientArr.length; i++){
				pNamesandEmails[i]= patientArr[i].getName().toString() + " - " + patientArr[i].getEmail();
			}
			
			String chosen= (String)JOptionPane.showInputDialog(null, "Choose a patient.", "Delete Patient", JOptionPane.QUESTION_MESSAGE, null, pNamesandEmails, pNamesandEmails[0]);
			if(chosen != null){
				int index= this.getChosenIndex(pNamesandEmails, chosen);
				System.out.println(index);
				
				Patient p= patientList.get(index);
				
				System.out.println(p.getEmail());
				
				TreatmentRecordPresenter presRec = new TreatmentRecordPresenter(p, dba );
				presRec.disableDocOrder();

			}
		}
		
		else if( event.getSource() == myView.getAppointmentButton()){
			
			
			Object[] options = {"View", "Edit", "Delete", "Cancel"};
			int option= JOptionPane.showOptionDialog(null,"What would you like to do to the appointment?", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); //0-view, 1-edit, 2-delete, 3-cancel

			if(option == 0)
			{
				ViewAppointmentPresenter pres= new ViewAppointmentPresenter(0, null, new ViewDailyAppointmentWindow());
			}
			else if(option == 1)
			{
				ViewAppointmentPresenter pres= new ViewAppointmentPresenter(1, null, new ViewDailyAppointmentWindow());
			}
			else if(option == 2)
			{
				ViewAppointmentPresenter pres= new ViewAppointmentPresenter(2, null, new ViewDailyAppointmentWindow());
			}
			else{
				//do nothing. the sys admin canceled the task
			}
		}
		else{
			System.out.println("Unknown action");
			//do nothing. the sys admin canceled the task
		}
	
	}
	
	private int getChosenIndex(String[] arr, String target){
		int i=0;
		boolean found= false;
		
		while(!found){
			if(arr[i].equals(target)){
				found= true;
			}
			else{
				i++;
			}
		}
		return i;
	}

}
